package com.yztc.utils;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.yztc.chunboapp.R;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;


public class ShareLog {


    public  ShareLog(final Context mContext, final String path) {

        //1、分享的初始化
        ShareSDK.initSDK(mContext);

        Log.i("wx", "showShareDialog");

        View view = LayoutInflater.from(mContext).inflate(R.layout.shareview_item, null);
        // 设置style 控制默认dialog带来的边距问题
        final Dialog dialog = new Dialog(mContext, R.style.common_dialog);
        dialog.setContentView(view);
        dialog.show();

        // 监听
        View.OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(View v) {

                switch (v.getId()) {

                    case R.id.view_share_weixin:
                        // 分享到微信
                        //2、设置分享内容
                        Wechat.ShareParams sp1 = new Wechat.ShareParams();
                        sp1.setShareType(Platform.SHARE_WEBPAGE);//非常重要：一定要设置分享属性
                        sp1.setTitle("分享测试");  //分享标题
                        sp1.setText("随便测试下，不要当真~");   //分享文本
                        sp1.setImageUrl("http://img0.imgtn.bdimg.com/it/u=3520887839,4032241847&fm=21&gp=0.jpg");//网络图片rul
                        sp1.setUrl(path);   //网友点进链接后，可以看到分享的详情

                        //3、非常重要：获取平台对象
                        Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
                        //wechat. setPlatformActionListener(MainActivity.this); // 设置分享事件回调
                        // 执行分享
                        wechat.share(sp1);
                        break;

                    case R.id.view_share_pengyou:
                        // 分享到朋友圈
                        //2、设置分享内容
                        WechatMoments.ShareParams sp2 = new WechatMoments.ShareParams();
                        sp2.setShareType(Platform.SHARE_WEBPAGE); //非常重要：一定要设置分享属性
                        sp2.setTitle("分享测试");  //分享标题
                        sp2.setText("随便测试下，不要当真~");   //分享文本
                        sp2.setImageUrl("http://img0.imgtn.bdimg.com/it/u=3520887839,4032241847&fm=21&gp=0.jpg");//网络图片rul
                        sp2.setUrl(path);   //网友点进链接后，可以看到分享的详情

                        //3、非常重要：获取平台对象
                        Platform wechatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
                        //wechatMoments.setPlatformActionListener(MainActivity.this); // 设置分享事件回调
                        // 执行分享
                        wechatMoments.share(sp2);
                        break;

                    case R.id.view_share_qq:
                        // 分享到QQ
                        //2、设置分享内容
                        QQ.ShareParams sp3 = new QQ.ShareParams();
                        sp3.setTitle("我是分享标题");
                        sp3.setText("我是分享文本，啦啦啦~http://uestcbmi.com/");
                        sp3.setImageUrl("http://img0.imgtn.bdimg.com/it/u=3520887839,4032241847&fm=21&gp=0.jpg");//网络图片rul
                        sp3.setTitleUrl(path);  //网友点进链接后，可以看到分享的详情
                        //3、非常重要：获取平台对象
                        Platform qq = ShareSDK.getPlatform(QQ.NAME);
                        //qq.setPlatformActionListener(MainActivity.this); // 设置分享事件回调
                        // 执行分享
                        qq.share(sp3);
                        break;

                    case R.id.view_share_weibo:
                        // 分享到微博
                        //2、设置分享内容
                        SinaWeibo.ShareParams sp4 = new SinaWeibo.ShareParams();
                        sp4.setText("我是分享文本，啦啦啦~http://uestcbmi.com/"); //分享文本
                        sp4.setImageUrl("http://img0.imgtn.bdimg.com/it/u=3520887839,4032241847&fm=21&gp=0.jpg");//网络图片rul

                        //3、非常重要：获取平台对象
                        Platform sinaWeibo = ShareSDK.getPlatform(SinaWeibo.NAME);
                        //sinaWeibo.setPlatformActionListener(MainActivity.this); // 设置分享事件回调
                        // 执行分享
                        sinaWeibo.share(sp4);
                        break;

                    case R.id.view_share_link:
                        // 复制分享链接
                        //onShare2Weixin();
                        Toast.makeText(mContext, "已将链接复制到剪切板，您可以自行分享给好友了", Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.share_cancel_btn:
                        // 取消
                        break;

                }

                dialog.dismiss();
            }

        };
        ViewGroup mViewWeixin = (ViewGroup) view.findViewById(R.id.view_share_weixin);
        ViewGroup mViewPengyou = (ViewGroup) view.findViewById(R.id.view_share_pengyou);
        ViewGroup mViewQQ = (ViewGroup) view.findViewById(R.id.view_share_qq);
        ViewGroup mViewWeibo = (ViewGroup) view.findViewById(R.id.view_share_weibo);
        ViewGroup mViewLink = (ViewGroup) view.findViewById(R.id.view_share_link);
        TextView mBtnCancel = (TextView) view.findViewById(R.id.share_cancel_btn);
        mViewWeixin.setOnClickListener(listener);
        mViewPengyou.setOnClickListener(listener);
        mViewQQ.setOnClickListener(listener);
        mViewWeibo.setOnClickListener(listener);
        mViewLink.setOnClickListener(listener);
        mBtnCancel.setOnClickListener(listener);

        // 设置相关位置，一定要在 show()之后
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(50, 100, 50, 100);//左，上，右，下
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);

    }

}
